package com.wen.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.edu.api.VodApi;
import com.wen.edu.entity.EduVideo;
import com.wen.edu.mapper.EduVideoMapper;
import com.wen.edu.service.EduVideoService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author wen
 * @since 2021-05-29
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    @Resource
    private VodApi vodApi;

    @Override
    public void removeByCourseId(String courseId) {
        //        先查询该课程下所有的小节对应的视频id
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
//        此处查询了所有字段信息,可以优化为只查询视频id字段
        List<EduVideo> eduVideos = baseMapper.selectList(wrapper);
        List<String> list = new ArrayList<>();
        for (EduVideo eduVideo : eduVideos) {
            if (!StringUtils.isEmpty(eduVideo.getVideoSourceId()))
                list.add(eduVideo.getVideoSourceId());
        }
//        先删除阿里云中的视频(如果该课程下没有任何视频,就不调用删除视频的方法)
        if (list.size() != 0)
            vodApi.deleteMultiAlyVideo(list);
//        再删除小节信息
        baseMapper.delete(wrapper);
    }

}
